﻿using System.Security.Cryptography;
using System.Text;

namespace LicenseApp
{
    public class EncodeHelper
    {
        /// <summary>
        /// 按指定键值进行AES解密
        /// </summary>
        /// <param name="strContent">要解密字符</param>
        /// <param name="strKey">加密时使用的键值 Unicode编码 必须大于或等于8个英文或汉字</param>
        /// <returns></returns>
        public static string AESDecrypt(string strContent, string strKey,string aesIV_128)
        {
            if (string.IsNullOrEmpty(strContent)) return string.Empty;
            if (strKey.Length > 8) strKey = strKey.Substring(0, 8);
            //与加密时Base64对应
            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(strContent);
                //解密
                using (var aesAlg = Aes.Create())
                {
                    aesAlg.Key = Encoding.Unicode.GetBytes(strKey);
                    aesAlg.IV = Encoding.Unicode.GetBytes(aesIV_128);

                    // Create a decryptor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    // Create the streams used for decryption.
                    using (MemoryStream msDecrypt = new MemoryStream(byEnc))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                return srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 按指定键值进行AES加密
        /// </summary>
        /// <param name="plainText">要解密字符</param>
        /// <param name="strKey">加密时使用的键值 Unicode编码 必须大于或等于8个英文或汉字</param>
        /// <returns></returns>
        public static string AES(string strContent, string strKey, string aesIV_128)
        {
            if (string.IsNullOrEmpty(strContent)) return string.Empty;
            if (strKey.Length > 8) strKey = strKey.Substring(0, 8);

            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = Encoding.Unicode.GetBytes(strKey);
                aesAlg.IV = Encoding.Unicode.GetBytes(aesIV_128);
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(strContent);
                        }
                        return Convert.ToBase64String(msEncrypt.ToArray()); //返回Base64密文方便传输
                    }
                }
            }
        }
    }
}
